package com.example.controller;


import com.example.service.ImageService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ObjectController {

    @Resource
    ImageService imageService;


    @GetMapping("/images/**")
    public void imageFetch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Content-Type", "image/jpeg");
        this.fetchImage(request, response);

    }

    private void fetchImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String imagePath = request.getServletPath().substring(7);
        ServletOutputStream outputStream = response.getOutputStream();
        //获取一个图像文件，并将其内容写入到提供的 OutputStream 中
        imageService.fetchImageFromMinio(outputStream, imagePath);
        response.setHeader("Cache-Control", "max-age=2592000");
        response.setHeader("Content-Type", "image/jpeg");
    }

}
